﻿using Elsa.JavaScript.Models;
using Elsa.Workflows;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.SPI;

namespace T.FAS.Runtime.MF.Activities.Nodes
{
    internal class IfNode : IfElseNode, INodeMapper
    {
        public new IActivity MapToActivity()
        {
            var activity = new IfActivity() { Id = Id, Name = Data.Label };
            var conditions = new List<BranchCondition>();
            var conditionDetails = new Dictionary<string, List<ConditionDetail>>();
            var ifElseCases = new List<string> { "if", "else" };

            foreach (var item in Data.Cases)
            {
                conditions.Add(new BranchCondition { Label = item.CaseID, Condition = JavaScriptExpression.Create(ParseJavaScriptExpression(item)) });
            }

            foreach (var item in Data.Cases)
            {
                if (item.Conditions == null) continue;
                conditionDetails.Add(item.CaseID, ParseConditionDetail(item.Conditions));
            }
            activity.Conditions = conditions;
            activity.ConditionDetails = conditionDetails;
            activity.Metadata = DesignerMeta;

            return activity;
        }
    }
}
